unit USerjUtils;

interface

uses
  Windows, RegularExpressions;

  function WordToStr(const S: word): string;
  function StrToWord(const S: string): word;

  function IsIPAdress(s: string): boolean;

implementation

function WordToStr(const S: word): string;
  {$IFDEF USE_INLINE} inline; {$ENDIF}
begin
  {$IFDEF STRING_IS_UNICODE}
  Result := BytesToStringRaw(ToBytes(Value));
  {$ELSE}
  SetLength(Result, SizeOf(S));
  Move(S, Result[1], SizeOf(S));
  {$ENDIF}
end;

function StrToWord(const S: String): Word;
{$IFDEF USE_INLINE} inline; {$ENDIF}
begin
  if Length(S) > 1 then begin
    {$IFDEF STRING_IS_UNICODE}
    Result := TwoCharToWord(Value[1], Value[2]);
    {$ELSE}
    Result := PWord(Pointer(S))^;
    {$ENDIF}
  end else begin
    Result := 0;
  end;
end;

function IsIPAdress(s: string): boolean;
const
  ipRegExp: string =  '\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'+
                      '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'  +
                      '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.'  +
                      '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b';
                      // pattern '0.0.0.0 - 255.255.255.255'
begin
  result:=TRegEx.IsMatch(s, ipRegExp);
end;


end.
